﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessModel;
using EntityDataModel;
using System.ComponentModel;
using System.Data.Objects.DataClasses;
using System.Data;
using BaseUtilities;

namespace Controller.DataProvider
{
    public class BuyOrderPro : GenericProvider<BuyOrder>
    {
        public BuyOrderPro()
        {
            EntityName = "Tbl_BuyOrder";
        }

        #region override Methods
        //----------------------------------------
        public override EntityObject CreateEntity()
        {
            return new Tbl_BuyOrder();
        }
        //----------------------------------------
        public override BuyOrder CreateModel()
        {
            return new BuyOrder();
        }
        //----------------------------------------
        public override void ToEntity(BuyOrder pModel, EntityObject pEntity, params EnumFieldCopy[] pEnumFieldCopys)
        {
            Tbl_BuyOrder oEntity = pEntity as Tbl_BuyOrder;
            oEntity.BuyerId = pModel.BuyerId;
            oEntity.BuyerUsername = pModel.BuyerUsername;
            pEnumFieldCopys.ToList().ForEach((item) =>
            {
                switch (item)
                {
                    case EnumFieldCopy.Id:
                        oEntity.Id = pModel.Id;
                        break;
                    case EnumFieldCopy.Created:
                        oEntity.CreatedBy = pModel.CreatedBy;
                        oEntity.CreatedDate = pModel.CreatedDate;
                        break;
                    case EnumFieldCopy.Childs:
                        BuyOrderDetailPro detailPro = new BuyOrderDetailPro();
                        pModel.BuyOrderDetails.ToList().ForEach((child) =>
                            {
                                Tbl_BuyOrderDetail detail = oEntity.Tbl_BuyOrderDetail.Where(p => p.Id == child.Id).FirstOrDefault();
                                if (detail == null)
                                {
                                    detail = new Tbl_BuyOrderDetail();
                                    oEntity.Tbl_BuyOrderDetail.Add(detail);
                                }
                                detailPro.ToEntity(child, detail, EnumFieldCopy.All);
                            });
                        break;
                    case EnumFieldCopy.All:
                        oEntity.Id = pModel.Id;
                        oEntity.CreatedBy = pModel.CreatedBy;
                        oEntity.CreatedDate = pModel.CreatedDate;
                        break;
                }
            });
        }
        //----------------------------------------
        public override void ToModel(EntityObject pEntity, BuyOrder pModel, params EnumFieldCopy[] pEnumFieldCopys)
        {
            Tbl_BuyOrder oEntity = pEntity as Tbl_BuyOrder;
            pModel.BuyerId = oEntity.BuyerId;
            pModel.BuyerUsername = oEntity.BuyerUsername;
            pEnumFieldCopys.ToList().ForEach((item) =>
            {
                switch (item)
                {
                    case EnumFieldCopy.Id:
                        pModel.Id = oEntity.Id;
                        break;
                    case EnumFieldCopy.Created:
                        pModel.CreatedBy = oEntity.CreatedBy;
                        pModel.CreatedDate = oEntity.CreatedDate;
                        break;
                    case EnumFieldCopy.Childs:
                        BuyOrderDetailPro detailPro = new BuyOrderDetailPro();
                        oEntity.Tbl_BuyOrderDetail.ToList().ForEach((child) =>
                        {
                            BuyOrderDetail detail = pModel.BuyOrderDetails.Where(p => p.Id == child.Id).FirstOrDefault();
                            if (detail == null)
                            {
                                detail = new BuyOrderDetail();
                                pModel.BuyOrderDetails.Add(detail);
                            }
                            detailPro.ToModel(child, detail, EnumFieldCopy.All);
                        });
                        break;
                    case EnumFieldCopy.All:
                        pModel.Id = oEntity.Id;
                        pModel.CreatedBy = oEntity.CreatedBy;
                        pModel.CreatedDate = oEntity.CreatedDate;
                        break;
                }
            });
        }
        #endregion override Methods

        //----------------------------------------
        public ResultModel SaveBuyOrderFull(BuyOrder pBuyOrder)
        {
            ResultModel oResultModel = new ResultModel();
            oResultModel.HasError = true;
            using (ManageStoreEntities entities = new ManageStoreEntities())
            {
                Tbl_BuyOrder oEntity = entities.Tbl_BuyOrder.Where(p => p.Id == pBuyOrder.Id).FirstOrDefault();
                if (oEntity == null)
                {
                    oEntity = new Tbl_BuyOrder();
                    ToEntity(pBuyOrder, oEntity, EnumFieldCopy.Created, EnumFieldCopy.Modified);
                    entities.AddObject(EntityName, oEntity);
                }
                else
                {
                    ToEntity(pBuyOrder, oEntity, EnumFieldCopy.Modified);
                }
                // OrderDeatail.
                BuyOrderDetailPro oBuyOrderDetailPro = new BuyOrderDetailPro();
                ProductPro oProductPro = new ProductPro();
                pBuyOrder.BuyOrderDetails.ToList().ForEach((child) =>
                            {
                                // Product.
                                Tbl_Product oProduct = entities.Tbl_Product.Where(p => p.Id == child.ProductId).FirstOrDefault();
                                if (oProduct == null)
                                {
                                    oProduct = new Tbl_Product();
                                    oProductPro.ToEntity(child.Product, oProduct, EnumFieldCopy.Created, EnumFieldCopy.Modified);
                                }
                                else
                                {
                                    int? iQuantity = child.Quantity;
                                    //entities.ObjectStateManager.ChangeObjectState(oProduct, EntityState.Modified);
                                    // UnitChange.
                                    IList<Tbl_UnitChange> unitChanges = null;
                                    foreach (UnitChange item in child.Product.UnitChanges)
                                    {
                                        if (unitChanges == null)
                                            unitChanges = oProduct.Tbl_UnitChange.Where(p => p.ApplyDate.HasValue && item.ApplyDate.HasValue && p.ApplyDate.Value.Date == item.ApplyDate.Value.Date).ToList();
                                        Tbl_UnitChange unitChange = null;
                                        if (unitChanges != null)
                                            unitChange = unitChanges.Where(p => p.Level == item.Level).FirstOrDefault();
                                        if (unitChange == null)
                                        {
                                            unitChange = new Tbl_UnitChange();
                                            oProduct.Tbl_UnitChange.Add(unitChange);
                                        }
                                        unitChange.UnitId = item.UnitId;
                                        unitChange.Quantity = item.Quantity;
                                        unitChange.SalePriceUnit = item.SalePriceUnit;
                                        unitChange.Level = item.Level;
                                        unitChange.ApplyDate = item.ApplyDate;

                                        //unitChange.ApplyDate = child.Product.ApplyDate;

                                        //if (item.Level == 0)
                                        //{
                                        //    unitChange.UnitId = child.UnitId;
                                        //    unitChange.Quantity = child.Quantity;
                                        //    unitChange.SalePriceUnit = child.SalePriceUnit;
                                        //}
                                        //if (item.Level == 0)
                                        //{
                                        //    unitChange.UnitId = oProduct.UnitId;
                                        //    unitChange.Quantity = 1;
                                        //}
                                        //// Unit Change.
                                        //if (item.UnitId == child.UnitId)
                                        //{
                                        //    iQuantity *= item.Quantity;
                                        //}
                                    }
                                    if (!oProduct.Quantity.HasValue)
                                        oProduct.Quantity = 0;
                                    //oProduct.Quantity += iQuantity;
                                    oProduct.Quantity += child.Quantity;
                                    oProduct.SalePriceUnit = child.SalePriceUnit;
                                }
                                // BuyOrderDetail.
                                Tbl_BuyOrderDetail oDetail = entities.Tbl_BuyOrderDetail.Where(p => p.Id == child.Id).FirstOrDefault();
                                if (oDetail == null)
                                {
                                    oDetail = new Tbl_BuyOrderDetail();
                                    oBuyOrderDetailPro.ToEntity(child, oDetail, EnumFieldCopy.Created, EnumFieldCopy.Modified);
                                    oDetail.Tbl_BuyOrder = oEntity;
                                    oDetail.Tbl_Product = oProduct;
                                }
                                else
                                {
                                    oBuyOrderDetailPro.ToEntity(child, oDetail, EnumFieldCopy.Modified);
                                }
                            });
                entities.SaveChanges();
                oResultModel.HasError = false;
                oResultModel.Result = oEntity.EntityKey.EntityKeyValues.Single().Value;
            }
            return oResultModel;
        }
    }
}
